package com.csvcn.android.retrofit;

import android.util.Log;

import com.csvcn.android.constant.Consts;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class HttpManager {
    private static HttpManager instance = null;

    private HttpGetService mHttpGetService = null;

    private HttpPostService mHttpPostService = null;

    private Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(Consts.BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .build();

    public HttpManager getInstance() {
        if (instance == null) {
            instance = new HttpManager();
        }
        return instance;
    }

    private HttpManager() {
        mHttpGetService = retrofit.create(HttpGetService.class);
        mHttpPostService = retrofit.create(HttpPostService.class);
    }

    public void listRepos(String user) {
        Call<ResponseBody> repos = mHttpGetService.listRepos(user);
        repos.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                Log.e("APP",response.body().source().toString());
            }
            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                t.printStackTrace();
            }
        });
    }

    public void query(String type, String postid) {
        HttpPostService apiService = retrofit.create(HttpPostService.class);
        Call<SearchRespBody> call = apiService.search(type, postid);
        call.enqueue(new Callback<SearchRespBody>(){
            @Override
            public void onResponse(Call<SearchRespBody> call,Response<SearchRespBody> response){
                Log.e("APP",response.body().toString());
            }
            @Override
            public void onFailure(Call<SearchRespBody> call,Throwable t){
                t.printStackTrace();
            }
        });
    }
}
